//    Copyright (C) Kherty.  All rights reserved.

using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Effects;
using OpenLS.Spreadsheet.UI.Internals;


namespace OpenLS.Spreadsheet.UI
{
    partial class CommentResources
    {
        public CommentResources()
        {
            InitializeComponent();
        }

        private static CommentResources _instance;
        internal static CommentResources Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new CommentResources();
                return _instance;
            }
        }

        private void root_GotFocus(object sender, RoutedEventArgs e)
        {
            FrameworkElement element = (FrameworkElement)sender;
            Comment comment = (Comment) element.DataContext;
            AdornerLayer layer = AdornerLayer.GetAdornerLayer(element);
            FrameworkElement el = (FrameworkElement) element.TemplatedParent;
            Contract.Assert(el is ContentPresenter);
            ResizeAdorner adorner = new ResizeAdorner(el);
            element.Tag = adorner;
            adorner.DragSideDelta += delegate(object sender_, DragSideDeltaEventArgs args)
                                         { OnDragSide(args, comment); };
            adorner.DragDelta += delegate(object sender_, DragDeltaEventArgs args)
            { OnDragDelta(args, comment); };
            layer.Add(adorner);
            e.Handled = true;
        }

        static void OnDragDelta(DragDeltaEventArgs e, Comment comment)
        {
            comment.LeftMargin += e.HorizontalChange;
            comment.TopMargin += e.VerticalChange;
        }

        static void OnDragSide(DragSideDeltaEventArgs args, Comment comment)
        {
            switch (args.DragSide)
            {
                case Dock.Right:
                    comment.Width += args.HorizontalChange;
                    break;
                case Dock.Bottom:
                    comment.Height += args.VerticalChange;
                    break;
                case Dock.Left:
                    comment.Width -= args.HorizontalChange;
                    comment.LeftMargin += args.HorizontalChange;
                    break;
                case Dock.Top:
                    comment.Height -= args.HorizontalChange;
                    comment.TopMargin += args.VerticalChange;
                    break;
                default:
                    throw new NotSupportedException();
            }
        }

        private void RootLostFocus(object sender, RoutedEventArgs e)
        {
            TextBox element = (TextBox) sender;
            AdornerLayer layer = AdornerLayer.GetAdornerLayer(element);
            Adorner adorner = element.Tag as Adorner;
            if (layer != null)
                layer.Remove(adorner);
            CommentElement.SetIsEdited(element, false);
        }

        private void RootKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                DependencyObject textBox = (FrameworkElement)sender;
                FocusElement.GetNextFocusedElement(textBox).Focus();
                e.Handled = true;
            }
        }
#if DEBUG
        private static int counter;
#endif
        private void root_Loaded(object sender, RoutedEventArgs e)
        {
#if DEBUG
            Debug.WriteLine("comment loaded" + counter++);
#endif
            if (!BrowserInteropHelper.IsBrowserHosted)
            {
#if NET40
                ((TextBox) sender).Effect = new DropShadowEffect()
                                                      {
                                                          Color = Colors.Black
                                                      };
#else
                ((TextBox) sender).BitmapEffect = new DropShadowBitmapEffect
                                                      {
                                                          Color = Colors.Black
                                                      };
#endif
            }
        }
    }

}
